package com.online.customerchatserver.service.impl;


import com.online.customerchatserver.entity.Message;
import com.online.customerchatserver.service.MessageService;
import com.online.customerchatserver.slave0.Message0Mapper;
import com.online.customerchatserver.slave1.Message1Mapper;
import com.online.customerchatserver.thread.SpringApplicationContextHolder;
import com.online.customerchatserver.thread.ThreadLock;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by littleheng on 2018/5/2
 */
@Service
public class MessageServiceImpl implements MessageService {

    public SqlSessionFactory getSqlSessionFactory0() {
        return (SqlSessionFactory) SpringApplicationContextHolder.getApplicationContext().getBean("slave0SqlSessionFactory");
    }

    public SqlSessionFactory getSqlSessionFactory1() {
        return (SqlSessionFactory) SpringApplicationContextHolder.getApplicationContext().getBean("slave1SqlSessionFactory");
    }

    private SqlSession getSession(SqlSessionFactory sqlSessionFactory) {
        return sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    }


    @Override
    public void saveCustomerMessage(String uuid, Message message) {
        ThreadLock.messagesMap.put(uuid, message);
    }

    @Override
    public void saveAgentMessage(String uuid, Message message) {
        ThreadLock.agentMessagesMap.put(uuid, message);
    }

    @Override
    public void saveUuid(String[] uuids, int eid) {
        for (String uuid : uuids) {
            List<Object> list = new ArrayList<Object>();
            list.add(uuid);
            list.add(eid);
            ThreadLock.uuidsQueue.offer(list);
        }
    }

    @Override
    public void addBatchMessage(List<Message> messages) {
        SqlSession session0 = getSession(getSqlSessionFactory0());
        SqlSession session1 = getSession(getSqlSessionFactory1());
        Message0Mapper message0Mapper = session0.getMapper(Message0Mapper.class);
        Message1Mapper message1Mapper = session1.getMapper(Message1Mapper.class);
        try {
            int len = messages.size();
            for (int i = 0; i < len; i++) {
                Message message = messages.get(i);
                if (Integer.parseInt(message.getCompanyId()) % 2 == 0) {
                    message0Mapper.insertMessage(message);
                } else {
                    message1Mapper.insertMessage(message);
                }
                if (i % len == 0 || i == len - 1) {
                    session0.commit();
                    session0.clearCache();
                    session1.commit();
                    session1.clearCache();
                }
            }
        } catch (Exception e) {
            session0.rollback();
            session1.rollback();
            System.out.println(e.getMessage());
        } finally {
            session0.close();
            session1.close();
        }
    }

    @Override
    public void modifyBatchMessageStatus(List<String[]> uuids) {
        SqlSession session0 = getSession(getSqlSessionFactory0());
        SqlSession session1 = getSession(getSqlSessionFactory1());
        Message0Mapper message0Mapper = session0.getMapper(Message0Mapper.class);
        Message1Mapper message1Mapper = session1.getMapper(Message1Mapper.class);
        try {
            int len = uuids.size();
            for (int i = 0; i < len; i++) {
                String[] uuid = uuids.get(i);
                if (Integer.parseInt(uuid[1]) % 2 == 0) {
                    message0Mapper.updateMessageStatus(uuid[0]);
                } else {
                    message1Mapper.updateMessageStatus(uuid[0]);
                }
                if (i % len == 0 || i == len - 1) {
                    session0.commit();
                    session0.clearCache();
                    session1.commit();
                    session1.clearCache();
                }

            }
        } catch (Exception e) {
            session0.rollback();
            session1.rollback();
            System.out.println(e.getMessage());
        } finally {
            session0.close();
            session1.close();
        }
    }
}